package com.yyfax.ph.home.recommendloan.creditinfo;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yyfax.ph.R;

/**
 * Title: ChoosePictureDialog Description: Company: YYLending(YouJingPuHui)
 *
 * @author XiongYuFeng
 *         date 2016年8月19日 下午4:37:00
 */
public class ChoosePictureDialog extends Dialog implements OnClickListener, OnDismissListener {
	public interface ChoosePictureListener {
		void onTakePicture();

		void onChoosePictures();
	}

	@BindView(R.id.image)
	ImageView imageView;
	@BindView(R.id.horizon_divider)
	View mHorizonDivider;
	@BindView(R.id.take_picture_layout)
	TextView mTakePictureLayout;
	@BindView(R.id.choose_picture_layout)
	TextView mChoosePictureLayout;

	private ChoosePictureListener choosePictureListener;
	private int imageId;
	private boolean mOnlyChooseImageBtn = false;

	public ChoosePictureDialog(Context context, int theme) {
		super(context, theme);
	}

	public ChoosePictureDialog(Context context) {
		super(context, R.style.progress_style);
		setCancelable(true);
		setCanceledOnTouchOutside(true);
	}

	public ChoosePictureDialog(Context context, int imageId, ChoosePictureListener tempChoosePictureListener, boolean onlyChooseImageBtn) {
		this(context);
		this.imageId = imageId;
		this.choosePictureListener = tempChoosePictureListener;
		mOnlyChooseImageBtn = onlyChooseImageBtn;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.base_choose_image_layout);
		ButterKnife.bind(this);
		initView();
	}

	private void initView() {
		if (mOnlyChooseImageBtn){
			mHorizonDivider.setVisibility(View.GONE);
			mTakePictureLayout.setVisibility(View.GONE);
		}else {
			mHorizonDivider.setVisibility(View.VISIBLE);
			mTakePictureLayout.setVisibility(View.VISIBLE);
		}
		imageView.setImageResource(imageId);
	}

	private void initWindowParams() {
		Window window = getWindow();
		if (window != null) {
			window.setWindowAnimations(R.style.AnimBottom);
			ColorDrawable dw = new ColorDrawable(0xb0000000);
			window.setBackgroundDrawable(dw);
			window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			LayoutParams params = window.getAttributes();
			params.alpha = 1.0f;
			window.setAttributes(params);
		}
	}

	@Override
	public void onDismiss(DialogInterface arg0) {
		if (getWindow() != null) {
			LayoutParams params = getWindow().getAttributes();
			params.alpha = 1f;
			getWindow().setAttributes(params);
		}
	}

	@OnClick({R.id.take_picture_layout, R.id.choose_picture_layout, R.id.dialog_close_iv})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.take_picture_layout:
				if (null != choosePictureListener) {
					choosePictureListener.onTakePicture();
				}
				dismiss();
				break;
			case R.id.choose_picture_layout:
				if (null != choosePictureListener) {
					choosePictureListener.onChoosePictures();
				}
				dismiss();
				break;
			case R.id.dialog_close_iv:
				dismiss();
				break;
		}
	}
}
